﻿using Quartz;
using System.Threading;
using System.Threading.Tasks;

namespace DataProcess.Core.Quartz.Listener
{
    public class TriggerListener : ITriggerListener
    {
        public string Name { get; } = nameof(TriggerListener);

#if NET40
        /// <summary>
        /// Trigger完成时调用
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="context"></param>
        /// <param name="triggerInstructionCode"></param>
        public void TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode)
        {
            // trigger 类型为   SimpleTriggerImpl
        }

        /// <summary>
        /// Trigger触发时调用
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="context"></param>
        public void TriggerFired(ITrigger trigger, IJobExecutionContext context)
        {

        }

        /// <summary>
        /// 错过触发时调用(例：线程不够用的情况下)
        /// </summary>
        /// <param name="trigger"></param>
        public void TriggerMisfired(ITrigger trigger)
        {

        }

        /// <summary>
        /// 是否否决工作执行  return ture 就不执行 return false 就执行
        /// 
        /// </summary>
        /// <param name="trigger"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public bool VetoJobExecution(ITrigger trigger, IJobExecutionContext context)
        {
            return false;
        }

#elif (NET48 || NETSTANDARD)

        public Task TriggerComplete(ITrigger trigger, IJobExecutionContext context, SchedulerInstruction triggerInstructionCode, CancellationToken cancellationToken = default)
        {
            // trigger 类型为   SimpleTriggerImpl
            return Task.CompletedTask;
        }

        public Task TriggerFired(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public Task TriggerMisfired(ITrigger trigger, CancellationToken cancellationToken = default)
        {
            return Task.CompletedTask;
        }

        public Task<bool> VetoJobExecution(ITrigger trigger, IJobExecutionContext context, CancellationToken cancellationToken = default)
        {
            return Task.FromResult(false);
        }
#endif
    }
}
